package com.cn.fenyu.servies;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.cn.fenyu.bean.active_info;
import com.cn.fenyu.bean.student_info;
import com.cn.fenyu.bean.student_infoExample;
import com.cn.fenyu.bean.update_info;
import com.cn.fenyu.bean.update_infoExample;
import com.cn.fenyu.dao.active_infoMapper;
import com.cn.fenyu.dao.student_infoMapper;
import com.cn.fenyu.dao.update_infoMapper;

@Service
public class MainServies {

	@Autowired
	student_infoMapper studao;
	@Autowired
	active_infoMapper activedao;
	@Autowired
	update_infoMapper updatedao;

	public int updateuser(student_info stu) {
		return studao.updateByPrimaryKey(stu);
	}

	public student_info finduser(String userid) {
		student_infoExample exp = new student_infoExample();
		exp.createCriteria().andStudentidEqualTo(userid);
		List<student_info> list = studao.selectByExample(exp);
		return list.size() == 1 ? list.get(0) : null;
	}

	public List<active_info> activelist() {
		return activedao.selectByExample(null);
	}

	public List<update_info> selectall() {
		return updatedao.showpage();
	}

	public List<update_info> sublistbyuser(String userid) {
		update_infoExample exmp = new update_infoExample();
		exmp.createCriteria().andStuIdEqualTo(userid);
		return updatedao.selectByExample(exmp);
	}

	public List<update_info> sublistbyactive(Integer active) {
		update_infoExample exmp = new update_infoExample();
		exmp.createCriteria().andActiveIdEqualTo(active);
		return updatedao.selectByExample(exmp);
	}

	public int insertUpdate(update_info up) {
		return updatedao.insertSelective(up);
	}

	public void submitdel(String userid, Integer activeId) {
		update_infoExample exmp = new update_infoExample();
		exmp.createCriteria().andActiveIdEqualTo(activeId);
		List<update_info> ups = updatedao.selectByExample(exmp);
		for (update_info update : ups) {
			if (update.getStuId().equals(userid)) {
				List<String> group = JSONArray.parseArray(update.getGroupJson(), String.class);
				for (String string : group) {
					for (int i = 0; i < ups.size(); i++) {
						if (ups.get(i).getStuId().equals(string)) {
							updatedao.deleteByPrimaryKey(ups.get(i).getPrimaryId());
						}
					}
				}
				updatedao.deleteByPrimaryKey(update.getPrimaryId());
			}
		}
	}
}
